package com.sn.shop.service.impl;

import com.sn.shop.model.Role;
import com.sn.shop.model.User;
import com.sn.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther sn
 * @date 2018/3/28
 * @description TODO
 */
public class CustomUserService implements UserDetailsService {

    @Autowired
    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userService.findByName(s);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        // TODO 把role添加进去
        List<Role> test = new ArrayList<>();
        test.add(new Role("admin"));
        test.add(new Role("test"));
        user.setRoles(test);
        return user;
    }
}
